package com.gxa.modules.payment.service.impl;

import com.gxa.modules.payment.entity.Meter;
import com.gxa.modules.payment.form.MeterForm;
import com.gxa.modules.payment.mapper.MeterMapper;
import com.gxa.modules.payment.mapper.PayItemMapper;
import com.gxa.modules.payment.service.MeterService;
import com.gxa.modules.payment.util.DocNoGenerationUtil;
import com.gxa.modules.payment.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MeterServiceImpl implements MeterService {

    @Autowired
    private MeterMapper meterMapper;

    @Autowired
    private PayItemMapper payItemMapper;

    @Autowired
    private DocNoGenerationUtil docNoGenerationUtil;

    @Override
    public List<Meter> queryByPage(Map<String, Object> params) {
        List<Meter> meters = this.meterMapper.queryByPage(params);
        return meters;
    }

//    @Override
//    public Integer addNo() {
//        Integer code = this.docNoGenerationUtil.generateDocNo(1);
//        return code;
//    }

    @Override
    public void meterAdd(MeterForm meterForm) {
        Integer code = this.docNoGenerationUtil.generateDocNo(1);

//        Integer code = meterForm.getCode();
//        Integer integer = this.payItemMapper.queryItemId(code);
        meterForm.setCode(code);

        Date addTime = new Date();
        meterForm.setAddTime(addTime);
//        meterForm.setItemId(integer);

        this.meterMapper.addMeter(meterForm);
    }

    @Override
    public List<Meter> queryAll(Integer code) {
        List<Meter> meters = this.meterMapper.queryAll(code);

        return meters;
    }

    @Override
    public void meterUpdate(MeterForm meterForm) {
        Integer code = meterForm.getCode();

        List<Meter> meters = this.meterMapper.queryAll(code);
        System.out.println(meters);

        for(Meter m1 : meters){
            Date enableTime1 = m1.getEnableTime();

            Date enableTime = meterForm.getEnableTime();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String strTime1 = sdf.format(enableTime1);
            String strTime = sdf.format(enableTime);

            if(m1.getStatus() != 1){
                if(strTime.equals(strTime1)){
                    meterForm.setStatus(1);
                }
//                else{
//                    return "修改的启用时间与创建时的启用时间不符，请重新修改";
//                }
            }
//            else{
//                return "仪表已启用，请勿重复启用";
//            }
        }
        this.meterMapper.updateMeter(meterForm);
    }

    @Override
    public void meterDelete(Integer code) {
        this.meterMapper.deleteMeter(code);
    }

    @Override
    public void meterDeleteIds(List<Integer> codes) {
        this.meterMapper.deleteMeterIds(codes);
    }
}
